<template>
  <div>
    <div style="background: #eaeaea">
      <div class="box">
        <div class="left">
          <div>欢迎您!</div>

          <div v-if="!getUserInfo">
            <router-link to="/login"> 请登录 </router-link>
          </div>

          <div v-if="!getUserInfo">
            <router-link to="/register"> 免费注册 </router-link>
          </div>

          <div v-if="getUserInfo">{{ getUserInfo }}</div>

          <div v-if="getUserInfo" @click="logout">|&nbsp;退出登录</div>
        </div>
        <div class="right">
          <div>
            <router-link to="/Center">我的订单</router-link>
          </div>
          <div>
            <router-link to="/ShopCart">我的购物车</router-link>
          </div>
          <!-- <div>我的尚品汇</div>
          <div>尚品汇会员</div>
          <div>企业采购</div>
          <div>关注尚品汇</div>
          <div>合作招商</div>
          <div>商家后台</div> -->
        </div>
      </div>
    </div>

    <div class="bot">
      <router-link to="/home">
        <img src="../../assets/logo.png" alt="" />
      </router-link>
      <div>
        <form action="###" class="seachBox">
          <input
            type="text"
            id="autocomplete"
            autocomplete="on"
            class="input-error input-xxlarge"
            v-model.trim="keyword"
          />
          <div @click="search">搜索</div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      keyword: "",
    };
  },
  computed: {
    ...mapState({
      getUserInfo: (state) => state.user.userinfo.name,
    }),
  },
  mounted() {
    this.$bus.$on("clear", () => {
      this.keyword = "";
    });
  },
  methods: {
    search() {
      //对象形式传参跳转(常用方式) 需要name开头
      //如果不传入动态路由中要求的字段的话跳转的时候的路径会出现问题,如果不想传入的话需要在路由页面加上一个问号
      //为空的话最好传入undefined,要不然路径会出错
      let loction = {
        name: "search",
        params: { keyword: this.keyword === "" ? undefined : this.keyword },
      };
      if (this.$route.query) {
        loction.query = this.$route.query;
      }
      this.$router.push(loction);
    },
    async logout() {
      try {
        await this.$store.dispatch("logout");
        this.$router.replace("/home");
      } catch (error) {
        console.error(error);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  background: #eaeaea;
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  width: 1200px;
  margin: 0 auto;

  .right,
  .left {
    display: flex;

    a {
      color: #000;
    }
  }

  .left > div:nth-child(1),
  .left > div:nth-child(2) {
    margin-right: 10px;
  }

  .right > div {
    margin-right: 10px;
  }
}

.bot {
  display: flex;
  justify-content: space-between;
  padding: 20px 0;
  align-items: center;
  width: 1200px;
  margin: 0 auto;

  img {
    display: block;
    width: 80px;
    height: 80px;
  }

  .seachBox {
    display: flex;

    input {
      width: 500px;
      height: 30px;
      border: 2px solid orangered;
      outline-color: orangered;
    }

    &:focus {
      outline: none;
    }

    div {
      height: 36px;
      width: 80px;
      color: #fff;
      background: orangered;
      text-align: center;
      line-height: 36px;
      cursor: pointer;
    }
  }
}
</style>